#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "image.h"
#include "resizedialog.h"
#include "filterdialog.h"
#include"painter.h"
#include"mylabel.h"
#include<QPushButton>
#include<QWidget>
#include<QAction>
#include<QFileDialog>
#include<QString>
#include<QDebug>
#include<QSlider>
#include<QLabel>
#include<QImage>
#include<QPixmap>
#include<QByteArray>
#include<QCheckBox>
#include<meanfilter.h>
#include<medianfilter.h>
#include<QColor>
#include<QColorDialog>

//#include"rotatedialog.h"
#include "cutdialog.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->checkBox->setCheckable(true);
    QAction *open = ui->open;
    img=nullptr;
    //    ui->label->setScaledContents(true);
    ui->label->setFrameShape (QFrame::Box);
    ui->label->setStyleSheet("border-width:2px;border-style: solid;border-color: rgb(0, 0, 0);");
    //打开图片
    connect(open,&QAction::triggered,this,[&](){


        QString filename = QFileDialog::getOpenFileName(this,"打开图片","C:\\Users\\23022\\Desktop\\homework\\pr_design\\ipt\\images",
                                                        tr("Images (*.bmp)"));
        if(filename.isNull())
        {
            return;
        }
        if(img!=nullptr){
            delete img;
            img = nullptr;
        }
        ui->bright->setValue(0);
        ui->contrast->setValue(100);
        ui->checkBox->setChecked(false);
        img = new Image(filename.toLatin1().data());
        origin_img[0] = new Image(*img);
        origin_img[1] = new Image(*img);
        //        ui->label->setPixmap(QPixmap::fromImage((QImage)img));
        img->ShowImage(ui->label);
    });

    //保存图片
    connect(ui->save,&QAction::triggered,[=](){
        if(img==nullptr)
            return;
        QString address = QFileDialog::getSaveFileName(this, tr("保存图片"),
                                                       "C:\\Users\\23022\\Desktop\\homework\\pr_design\\ipt\\images",
                                                       tr("Images (*.bmp)"));

        if(address.isNull())
        {
            return;
        }
        //        QImage qimg = ui->label->pixmap().toImage();
        //        Image img(*(ui->label));
        img->WriteBMP(address.toLatin1().data());
    });

    connect(ui->flip,&QPushButton::clicked,[=](){
        if(img==nullptr)
            return;
        img->Flip(0);
        delete origin_img[1];
        origin_img[1] =new Image(*img);
        img->ShowImage(ui->label);
    });
    connect(ui->flip1,&QPushButton::clicked,[=](){
        if(img==nullptr)
            return;
        img->Flip(1);
        delete origin_img[1];
        origin_img[1] =new Image(*img);
        img->ShowImage(ui->label);
    });

    connect(ui->cut,&QPushButton::clicked,[=](){
        if(img==nullptr)
            return;
        cutdialog dia(this);
        dia.exec();
        if(dia.result()==1)
        {
            img->Cut(dia.Getx1(),dia.Gety1(),dia.Getx2(),dia.Gety2());
            img->ShowImage(ui->label);
            ui->checkBox->setChecked(false);
            ui->checkBox->setCheckable(false);
            delete origin_img[1];
            origin_img[1] =new Image(*img);
        }

    });

    connect(ui->MeanFilter,&QPushButton::clicked,[=](){
        if(img==nullptr)
            return;
        filterdialog dia(this);
        dia.exec();
        if(dia.result()==1)
        {
            int size=dia.getSize();
            MeanFilter filter(size);
            Image img1=filter.Filtering(*img);
            img1.ShowImage(ui->label);
            delete img;
            img = new Image(img1);
            ui->checkBox->setChecked(false);
            ui->checkBox->setCheckable(false);
            delete origin_img[1];
            origin_img[1] =new Image(*img);
        }
    });

    connect(ui->MediaFilter,&QPushButton::clicked,[=](){
        if(img==nullptr)
            return;
        filterdialog dia(this);
        dia.exec();
        if(dia.result()==1)
        {
            int size=dia.getSize();
            MedianFilter filter(size);
            Image img1=filter.Filtering(*img);
            img1.ShowImage(ui->label);
            delete img;
            img = new Image(img1);
            delete origin_img[1];
            origin_img[1] =new Image(*img);
        }
    });
    connect(ui->gray,&QPushButton::clicked,[=](){
        if(img==nullptr)
            return;
        img->gray();
        img->ShowImage(ui->label);
        delete origin_img[1];
        origin_img[1] =new Image(*img);
    });

    connect(ui->edgeDetection,&QPushButton::clicked,[=](){
        if(img==nullptr)
            return;
        img->gray();
        MeanFilter filter(5);
        Image img1=filter.Filtering(*img);
        img1.ShowImage(ui->label);
        delete img;
        img = new Image(img1);
        img->sobel();
        img->ShowImage(ui->label);
        delete origin_img[1];
        origin_img[1] =new Image(*img);
    });

    //    connect(ui->rotate,&QPushButton::clicked,[=](){
    //        rotatedialog dia(this);
    //        dia.exec();
    //        if(dia.result()==1)
    //        {
    //           int num=dia.getNum();
    //           img->Rotate(num);
    //           img->ShowImage(ui->label);
    //        }

    //    });
    connect(ui->anti_rotate,&QPushButton::clicked,[=](){
        if(img==nullptr)
            return;
        img->Rotate(90);
        delete origin_img[1];
        origin_img[1] =new Image(*img);
        img->ShowImage(ui->label);
    });
    connect(ui->normal_rotate,&QPushButton::clicked,[=](){
        if(img==nullptr)
            return;
        img->Rotate(-90);
        delete origin_img[1];
        origin_img[1] =new Image(*img);
        img->ShowImage(ui->label);
    });
    connect(ui->resize,&QPushButton::clicked,[=](){
        //        Image img(ui->label->pixmap().toImage());
        if(img==nullptr)
            return;
        ResizeDialog  dia(this);
        //        dia->setAttribute(Qt::WA_DeleteOnClose);
        dia.SetHW(img->Height(),img->Width());
        dia.exec();
        //        qDebug()<<"out";
        if(dia.result()==1)
        {
            //            qDebug()<<"in if";
            int new_h=dia.GetHeight();
            int new_w = dia.GetWidth();
            if(new_h>img->Height()||new_w>img->Width())
            {
                delete img;
                img = new Image(*(origin_img[0]));
            }
            img->Resize(new_h,new_w);
            delete origin_img[1];
            origin_img[1] =new Image(*img);
            img->ShowImage(ui->label);
        }

    });

    void (QLabel::*set_num)(int) = &QLabel::setNum;

    connect(ui->bright,&QAbstractSlider::valueChanged,ui->bri_label,set_num);

    connect(ui->bright,&QAbstractSlider::valueChanged,[=](){
        if(img==nullptr)
            return;
        img->BrightContrast(ui->bright->value(),ui->contrast->value(),origin_img[1]);

        img->ShowImage(ui->label);

    });
    connect(ui->contrast,&QAbstractSlider::valueChanged,ui->con_label,set_num);
    connect(ui->contrast,&QAbstractSlider::valueChanged,[=](){
        if(img==nullptr)
            return;
        img->BrightContrast(ui->bright->value(),ui->contrast->value(),origin_img[1]);
        img->ShowImage(ui->label);
    });
    connect(ui->pushButton_4, &QPushButton::clicked, [=]() {
        if (img == nullptr)
            return;
        img->operator-();
        delete origin_img[1];
        origin_img[1] =new Image(*img);
        img->ShowImage(ui->label);
    });


    Painter* paint=new Painter;
    connect(ui->painter_r,&QSpinBox::valueChanged,[=](){
        paint->SetR(ui->painter_r->value());
        paint->Clear();
    });
    connect(ui->painter_color,&QPushButton::clicked,[=](){

        if (img == nullptr)
            return;
        QColor col=QColorDialog::getColor(Qt::white,this);
        if(col.isValid())
        {
            int r,g,b;
            col.getRgb(&r,&g,&b);
//            qDebug()<<"r="<<r;
//            qDebug()<<"g="<<g;
//            qDebug()<<"b="<<b;
            paint->SetRGB(r,g,b);
            paint->Clear();

        }
        else{
            qDebug()<<"there";
            return;
        }


    });
    connect(ui->checkBox,&QCheckBox::stateChanged,[=](){
        if (img == nullptr)
            return;
//        qDebug()<<"1";
        if(ui->checkBox->isChecked())
        {

            ui->label->SetisPaint(true,paint,img,origin_img);
        }
        else{
            ui->label->SetisPaint(false);
        }
    });

    connect(ui->redo,&QPushButton::clicked,[=](){
        if(img==nullptr)
            return;
        delete img;
        delete origin_img[1];
        origin_img[1]=new Image(*origin_img[0]);
        img = new Image(*origin_img[0]);
        img->ShowImage(ui->label);
        paint->Clear();
        ui->painter_r->setValue(1);
        ui->checkBox->setChecked(false);
//        delete paint;
//        paint = new Painter();
        ui->checkBox->setCheckable(true);
        ui->bright->setValue(0);
        ui->contrast->setValue(100);
    });

    //    MyLabel* mylabel;
    ////    mylabel=ui->label;
    //    connect(ui->label,&MyLabel::mousePressEvent,this,&MainWindow::Test);
    //    connect(ui->label,SIGNAL(MyLabel::mousePressEvent()),this,SIGNAL(MainWindow::Test()));
}

void MainWindow::Test(){
    qDebug()<<"test";
}


MainWindow::~MainWindow()
{
    delete ui;
}


